//
//  ActivityDetailView.m
//  DaHaiDouBan
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 Wang Dahai. All rights reserved.
//

#import "ActivityDetailView.h"
#import "UIImageView+WebCache.h"
#import "Singleton.h"
#import "DataBaseSingleton.h"
@implementation ActivityDetailView

- (void)dealloc
{
    [_titleLabel release];
    [_detailImageView release];
    [_timeImageView release];
    [_timeLabel release];
    [_owner_name_imageView release];
    [_owner_name_Label release];
    [_category_name_ImageView release];
    [_category_name_Label release];
    [_addressImageView release];
    [_addressLabel release];
    [_act_intro_Label release];
    [_contentLabel release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame activity:(Activity *)activity;
{
    
    self = [super initWithFrame:frame];
    if (self) {
        //标题
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 17, 325, 30)];
        [self addSubview:self.titleLabel];
        [self.titleLabel release];
      //  self.titleLabel.backgroundColor = [UIColor cyanColor];
        
        self.titleLabel.text = activity.title;
        
        //图片框
        self.detailImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 60, 110, 150)];
        [self addSubview:self.detailImageView];

        [self.detailImageView sd_setImageWithURL:[NSURL URLWithString:activity.image] placeholderImage:[UIImage imageNamed:@"picholder@2x"]];
        
        NSLog(@"image = %@",_activity.image);
        [_detailImageView release];

        
        
        //时间
        self.timeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 60, 20, 20)];
        self.timeImageView.image = [UIImage imageNamed:@"icon_date_blue@2x"];
        [self addSubview:self.timeImageView];
        [_timeImageView release];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 60, 200, 18)];
        self.timeLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.timeLabel];
        [_timeLabel release];
       // self.timeLabel.backgroundColor = [UIColor cyanColor];
        
        NSString *begin = activity.begin_time;
        NSString *end = activity.end_time;
        begin = [begin substringFromIndex:5];
        begin = [begin substringToIndex:11];
        end = [end substringFromIndex:5];
        end = [end substringToIndex:11];
        
        self.timeLabel.text = [NSString stringWithFormat:@"%@ -- %@",begin,end];
        
        //"name": "国家博物馆"
        self.owner_name_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 85, 20, 20)];
        self.owner_name_imageView.image = [UIImage imageNamed:@"icon_sponsor_blue@2x"];
        [self addSubview:self.owner_name_imageView];
        [_owner_name_imageView release];
        self.owner_name_Label = [[UILabel alloc] initWithFrame:CGRectMake(165, 85, 200, 18)];
        self.owner_name_Label.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.owner_name_Label];
        [_owner_name_Label release];
      //  self.owner_name_Label.backgroundColor = [UIColor cyanColor];
        
        self.owner_name_Label.text = [activity.owner objectForKey:@"name"];
        
        
        //类型
        self.category_name_ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 110, 20, 20)];
        self.category_name_ImageView.image = [UIImage imageNamed:@"icon_catalog_blue@2x"];
        [self addSubview:self.category_name_ImageView];
        [_category_name_ImageView release];
        
        self.category_name_Label = [[UILabel alloc] initWithFrame:CGRectMake(165, 110, 200, 18)];
        self.category_name_Label.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.category_name_Label];
        [_category_name_Label release];
       // self.category_name_Label.backgroundColor = [UIColor cyanColor];
        
        self.category_name_Label.text = activity.category_name;
        //地点
        self.addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 135, 20, 20)];
        self.addressImageView.image = [UIImage imageNamed:@"icon_spot_blue@2x"];
        [self addSubview:self.addressImageView];
        [_addressImageView release];
        
        self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 135, 200, 36)];
        self.addressLabel.numberOfLines = 0;
        self.addressLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.addressLabel];
        [_addressLabel release];
      //  self.addressLabel.backgroundColor = [UIColor cyanColor];
        
        self.addressLabel.text = activity.address;
        
        
        self.act_intro_Label = [[UILabel alloc] initWithFrame:CGRectMake(25, 220, 325, 40)];
        self.act_intro_Label.text = @"活动介绍";
        self.act_intro_Label.font = [UIFont systemFontOfSize:20];
        self.act_intro_Label.textColor = [UIColor grayColor];
        [self addSubview:self.act_intro_Label];
        [_act_intro_Label release];
       // self.act_intro_Label.backgroundColor = [UIColor cyanColor];
        
        
     //内容(下)
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 270, 325, 1200)];
        [self.contentLabel setFont:[UIFont systemFontOfSize:15]];
        self.contentLabel.numberOfLines = 0;
        [self addSubview:self.contentLabel];
        [_contentLabel release];
       // self.contentLabel.backgroundColor = [UIColor cyanColor];

        self.contentLabel.text = activity.content;
        [self.contentLabel sizeToFit];
        //收藏按钮
        self.collectButtonn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.collectButtonn.frame = CGRectMake(125,230 , 26, 24);
        [self.collectButtonn setBackgroundImage:[UIImage imageNamed:@"ic_star_half@2x"] forState:UIControlStateNormal];
        [self.collectButtonn setBackgroundImage:[UIImage imageNamed:@"ic_star_red@2x"] forState:UIControlStateSelected];
        [self addSubview:self.collectButtonn];
        [self.collectButtonn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        

        
    }
    return self;
    
}
-(void)buttonAction:(UIButton *)btn
{
    
    
//    if (self.collectButtonn.selected == NO) {
//        NSLog(@"收藏");
//        
//    }else
//    {
//        NSLog(@"未收藏");
//        
//    }
//    
//    self.collectButtonn.selected = ! self.collectButtonn.selected;
    
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    
    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"name"] isEqualToString:str]){
        [[DataBaseSingleton shareDataBase] openDB];
        [[DataBaseSingleton shareDataBase] createCollectionTable];
    
        Activity *activity = [[DataBaseSingleton shareDataBase] selectCollectionWithTitle:self.activity.title];
        
        if([activity.title isEqualToString:self.activity.title]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您已经收藏过此电影" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
           
      }
        
        else{
            UIImage *collectionImage = [UIImage imageNamed:@"ic_star_red@2x.png"];
            collectionImage = [collectionImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.collectButtonn setBackgroundImage:collectionImage forState:UIControlStateNormal];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜您成功收藏此电影" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
            
            [[DataBaseSingleton shareDataBase] insertCollectionTable:self.activity];
            
            
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您还没有登录,收藏失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
     
    }


}
//-(void)setActivity:(Activity *)activity{
//    if(_activity != activity){
//        [_activity release];
//        _activity = [activity retain];
//    }
//    self.titleLabel.text = _activity.title;
//    NSString *begin = _activity.begin_time;
//    NSString *end = _activity.end_time;
//    begin = [begin substringFromIndex:5];
//    begin = [begin substringToIndex:11];
//    end = [end substringFromIndex:5];
//    end = [end substringToIndex:11];
//    self.timeLabel.text = [NSString stringWithFormat:@"%@ -- %@",begin,end];
//    self.owner_name_Label.text = _activity.name;
//    self.category_name_Label.text = _activity.category_name;
//    self.addressLabel.text = _activity.address;
//    self.contentLabel.text = _activity.content;
//    
//    [self.detailImageView sd_setImageWithURL:[NSURL URLWithString:_activity.image] placeholderImage:[UIImage imageNamed:@"picholder"]];
//    
//    //  查询数据库,如果数据库中有此数据,则改成收藏标志
//    [[DataBaseSingleton shareDataBase] openDB];
//    Activity *act = [[DataBaseSingleton shareDataBase] selectCollectionWithTitle:_activity.title];
//    //if([act.title isEqualToString:_activity.title]){
//        [self.collectButtonn setBackgroundImage:[UIImage imageNamed:@"ic_star_red"] forState:UIControlStateNormal];
//        
//        
//        
//   // }
//    
//}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
